import poplib, base64, time
step = 2
numMessages = 0
while step != 1:
#while 1:
    time.sleep (1.0)
    try:
        M = poplib.POP3('pop.mail.ru',110)
        M.user('sh27')
        M.pass_('sh27')
        if numMessages == M.stat()[0]:
            step = step -1
            M.quit()
            time.sleep (15.0)
            continue
        numMessages = M.stat()[0]
    except poplib.error_proto, details:
        print 'Protocol Error:', details, '\n'
        step = step -1
        continue
    except:
        print 'Error: Connection with server is lost\n'
        step = step -1
        continue
    flag = 0
    res_string = ''
    result = []
    flog3=open("loging.txt", 'wb')#
    for i in range(numMessages):
        for j in M.retr(i+1)[1]:

            flog3.write(j)
            flog3.write('\r\n')

            if j[1:5] == 'ate:':
                temp_date = j
            if flag > 0:
                res_string = res_string+base64.b64decode(j)
                flag = flag-1
            if j[1:] == "ubject: MySpace Account Confirmation":
                flag = -1
            if flag == -1:
                if j[:36] == 'Content-Transfer-Encoding: base64':
                    result.append ([])
                    result[len(result)-1].append (temp_date)
                    flag = 7
    flag = len(result)*3
    flog3.close()#
    for i in range(len(res_string)):
        k = 0
        if (res_string[i:i+6] == 'E-mail') or (res_string[i:i+8] == 'Password') or (res_string[i:i+5] == 'http:'):
            while res_string[i+k]!= '\n':
                k = k+1
            flag = flag - 1
            result[flag/3].append (res_string[i:i+k-1])
    flog=open("log.txt", 'wb')
    for i in range(len(result)):
        for j in range(4):
            flog.write(repr(result[i][j]))
            flog.write("\r\n")
        flog.write("\r\n")
    flog.close()
    print 'complite'
    step = step -1
    M.quit()
    #unfineshd
    
    import ClientForm, urllib2
    request = urllib2.Request(
        "http://www.myspace.com")
    response = urllib2.urlopen(request)
    print response.geturl()
    forms = ClientForm.ParseResponse(response, backwards_compat=False)
    response.close()

    form = forms[1]
    form.set_all_readonly(False)
    form["ctl00$Main$SplashDisplay$ctl00$Email_Textbox"] = "sh323@mail.ru"
    form["ctl00$Main$SplashDisplay$ctl00$Password_Textbox"] = "p@ssword"

    request2 = form.click()  # urllib2.Request object
    try:
        response2 = urllib2.urlopen(request2)
    except urllib2.HTTPError, response2:
        pass

    print response2.geturl()
    print response2.read()  # body
    #
    req11 = urllib2.Request("http://collect.myspace.com/reloc.cfm?c=3&code=B5805CC8-07AE-4FC6-A0D6-C71935A2B124")
    response3 = urllib2.urlopen(req11)
    #possible error
    print response3.geturl()

    print response3.info()  # headers
    #print response3.read()  # body
    response3.close()
    response2.close()
